package com.life.utils;

import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileUploadUtils {

    /**
     * 创建文件夹
     *
     * @param uploadPath
     * @return
     */
    public static File getAbsoluteFile(String uploadPath) {
        // 文件上传的路径
        File file = new File(uploadPath);
        // 如果文件夹不存在则创建
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        return file;
    }

    /**
     * 上传文件
     *
     * @param file
     * @param path
     * @throws IOException
     */
    public static void upload(MultipartFile file, String path) throws IOException {
        try {
            file.transferTo(getAbsoluteFile(path));
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }

    /**
     * 删除文件
     *
     * @param path
     */
    public static void delete(String path) {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 重命名文件
     *
     * @param oldPath
     * @param newName
     */
    public static void rename(String oldPath, String newName) {
        String newPath = oldPath.substring(0, oldPath.lastIndexOf("/") + 1) + newName;
        File oldFile = new File(oldPath);
        File newFile = new File(newPath);
        if (oldFile.exists()) {
            oldFile.renameTo(newFile);
        }
    }

    /**
     * 获取文件md5值
     *
     * @param file
     * @return
     */
    public static String getFileMD5(MultipartFile file) {
        try (InputStream inputStream = file.getInputStream()) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[1024];
            int read;
            while ((read = inputStream.read(buffer)) != -1) {
                md.update(buffer, 0, read);
            }
            byte[] md5Bytes = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : md5Bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException | IOException e) {
            throw new RuntimeException("无法计算文件的MD5值", e);
        }
    }

    /**
     * 获取文件的前256KB的md5值
     *
     * @param file
     * @return
     */
    public static String getFirst256KBMD5(MultipartFile file) {
        try (InputStream inputStream = file.getInputStream()) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[1024];
            int read;
            int total = 0;
            while ((read = inputStream.read(buffer)) != -1) {
                md.update(buffer, 0, read);
                total += read;
                if (total > 256 * 1024) {
                    break;
                }
            }
            byte[] md5Bytes = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : md5Bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException | IOException e) {
            throw new RuntimeException("无法计算文件的MD5值", e);
        }
    }
}
